<?php


namespace app\api\controller;


use app\admin\model\Orders;
use app\admin\model\Records;
use app\ApiBaseController;

class Record extends ApiBaseController
{

    /**
     * 获取审核列表 status=0 待审核  status=1已通过  status=2被驳回
     */
    public function getRecordsList()
    {
        $status = $this->request->param('status/d', 0);
        if (!in_array($status, [0,1,2])){
            return result(1, '类型不存在');
        }
        $data = Records::where('uid', $this->uid)
            ->where('status', $status)
            ->hidden(['uid','order_id','delete_time'])->select();
        if ($data->isEmpty()){
            return result(0, '', []);
        }
        $orders = new Orders();
        foreach($data as $k => $v){
            $data[$k]['true_name'] = $this->user->true_name;
            if ($v['state'] == $v['new_state']){
                $data[$k]['state'] = $orders->getStateList()[$v['state']];
            }else{
                $data[$k]['state'] = $orders->getStateList()[$v['state']].' -> '. $orders->getStateList()[$v['new_state']];
            }
            if ($v['add_pay_amount'] > 0){
                $data[$k]['pay_amount'] = $v['pay_amount'].' + '.$v['add_pay_amount'];
            }
            unset($data[$k]['add_pay_amount']);
            unset($data[$k]['new_state']);
        }
        return result(0,'', $data);
    }

    /**
     * 获取单条审核记录
     */
    public function getRecord()
    {
        $id = $this->request->param('id/d');
        $data = Records::where('id', $id)->hidden(['uid','order_id','delete_time'])->find();
        if (empty($data)){
            return result(1,'记录不存在');
        }
        if ($data->uid <> $this->uid){
            return result(1,'只能操作自己的记录');
        }
        $data['true_name'] = $this->user->true_name;
        $orders = new Orders();
        if ($data['state'] == $data['new_state']){
            $data['state'] = $orders->getStateList()[$data['state']];
        }else{
            $data['state'] = $orders->getStateList()[$data['state']].' -> '. $orders->getStateList()[$data['new_state']];
        }
        if ($data['add_pay_amount'] > 0){
            $data['pay_amount'] = $data['pay_amount'].' + '.$data['add_pay_amount'];
        }
        unset($data['add_pay_amount']);
        unset($data['new_state']);
        return result(0, '', $data);
    }
}